package at.fhooe.swa.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialCap;
import org.jfree.chart.plot.dial.DialFrame;
import org.jfree.chart.plot.dial.DialLayer;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;

public class GaugeSensorPanel extends JPanel
{
	private String title = "";
	private String gaugeLabel = "";
	
	private double minVal = 0;
	private double maxVal = 0;
	
	private double startAngle = 0;
	private double extent = 0;
	
	private double majorTickIncrement = 1;
	private int minorTickCount = 0;
	
//	private Color backgroundColor = null;
//	private Color foregroundColor = null;
	
	private Color plotGradiantPanelColor1 = null;
	private Color plotGradiantPanelColor2 = null;
	
	
	
	
	// we will create that:
	private DialLayer scale = null; // will be DialStandardScale
	
	private List<DialLayer> additionalLayers = null; 
	
	
	
	private DialPlot dialPlot = null;
	private DefaultValueDataset dataset = null;
	
	private double posX = 0;
	private double posY = 0;
	private double height = 0;
	private double width = 0;
	
	
	
	public GaugeSensorPanel(String title, String gaugeLabel, double minVal,
			double maxVal, double startAngle, double extent, double majorTickIncrement, 
			int minorTickCount, 
//			Color backgroundColor, Color foreGroundColor, 
			Color plotGradientPanelColor1, Color plotGradientPanelColor2, 
			double posX, double posY, double width, double height, DefaultValueDataset dataset)
	{
		this.title = title;
		this.gaugeLabel = gaugeLabel;
		this.minVal = minVal;
		this.maxVal = maxVal;
		this.startAngle = startAngle;
		this.extent = extent;
		this.majorTickIncrement = majorTickIncrement;
		this.minorTickCount = minorTickCount;
//		this.backgroundColor = backgroundColor;
//		this.foregroundColor = foreGroundColor;
		this.plotGradiantPanelColor1 = plotGradientPanelColor1;
		this.plotGradiantPanelColor2 = plotGradientPanelColor2;
		this.posX = posX;
		this.posY = posY;
		this.width = width;
		this.height = height;
		this.dataset = dataset;
		
		init();
	}
	
	public GaugeSensorPanel(String title, String gaugeLabel, double minVal,
			double maxVal, double startAngle, double extent, double majorTickIncrement, 
			int minorTickCount, 
//			Color backgroundColor, Color foreGroundColor, 
			Color plotGradientPanelColor1, Color plotGradientPanelColor2, List<DialLayer> additionalLayers, 
			double posX, double posY, double width, double height, DefaultValueDataset dataset)
	{
		this(title, gaugeLabel, minVal, maxVal, startAngle, extent, majorTickIncrement, minorTickCount,
//				backgroundColor, foreGroundColor, 
				plotGradientPanelColor1, plotGradientPanelColor2, posX,
				posY, width, height, dataset);
		this.additionalLayers = additionalLayers;
		
		if (dialPlot != null)
		{
			for (DialLayer layer : additionalLayers)
				dialPlot.addLayer(layer);
		}
	}
	
	private void init()
	{
		dialPlot = new DialPlot();
		
		dialPlot.setView(posX, posY, width, height);
		dialPlot.setDataset(this.dataset);
		
		StandardDialFrame f1 = new StandardDialFrame();
		dialPlot.setDialFrame(f1);
        
        GradientPaint gp = new GradientPaint(new Point(), 
                plotGradiantPanelColor1, new Point(), 
                plotGradiantPanelColor2);
        DialBackground db = new DialBackground(gp);
        db.setGradientPaintTransformer(new StandardGradientPaintTransformer(
                GradientPaintTransformType.VERTICAL));
        dialPlot.setBackground(db);
        
        DialTextAnnotation annotation = new DialTextAnnotation(gaugeLabel);
        annotation.setFont(new Font("Dialog", Font.BOLD, 14));
        annotation.setRadius(0.7);
        
        dialPlot.addLayer(annotation);

//        DialValueIndicator dvi = new DialValueIndicator(0);
//        dialPlot.addLayer(dvi);
        
        
        StandardDialScale scale = new StandardDialScale(
        		minVal, maxVal, startAngle, extent, majorTickIncrement, minorTickCount);
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.20);
        scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
        dialPlot.addScale(0, scale);

        DialPointer needle = new DialPointer.Pointer();
        needle.setRadius(0.8);
        dialPlot.addLayer(needle);
        
        DialCap cap = new DialCap();
        cap.setRadius(0.10);
        dialPlot.setCap(cap);
        
        JFreeChart chart = new JFreeChart(dialPlot);
        chart.setTitle(title);
        
        ChartPanel cp = new ChartPanel(chart);
        
        cp.setPreferredSize(new Dimension(300, 320));
        this.setLayout(new BorderLayout());
        this.add(cp, BorderLayout.CENTER);
		
	}
	
	public void updateDataSet(double newVal)
	{
		this.dataset.setValue(newVal);
	}
	
}
